package com.sit.estatemanagementserver.dao;

import com.sit.estatemanagementserver.enity.Complaint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ComplaintDao {
    @Autowired
    DatabaseManager db;

    // 重构: 适配新的 Complaint 构造函数，增加 imageId
    private Complaint resultSetToComplaint(ResultSet rs) throws SQLException {
        return new Complaint(
                rs.getString(DatabaseContract.ComplaintEntry.COLUMN_WORK_ORDER_ID),
                rs.getString(DatabaseContract.ComplaintEntry.COLUMN_MESSAGE),
                rs.getString(DatabaseContract.ComplaintEntry.COLUMN_IMAGE_ID_FK),
                rs.getString(DatabaseContract.ComplaintEntry.COLUMN_OWNER_PHONE_FK)
        );
    }

    public boolean addComplaint(Complaint complaint) {
        // 重构: SQL语句增加 image_id 列
        String sql = "INSERT INTO " + DatabaseContract.ComplaintEntry.TABLE_NAME + " (" +
                DatabaseContract.ComplaintEntry.COLUMN_WORK_ORDER_ID + ", " +
                DatabaseContract.ComplaintEntry.COLUMN_MESSAGE + ", " +
                DatabaseContract.ComplaintEntry.COLUMN_IMAGE_ID_FK + ", " +
                DatabaseContract.ComplaintEntry.COLUMN_OWNER_PHONE_FK + ") VALUES (?, ?, ?, ?)";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, complaint.getWorkOrderId());
            pstmt.setString(2, complaint.getMessage());
            pstmt.setString(3, complaint.getImageId()); // 新增字段
            pstmt.setString(4, complaint.getOwnerPhone());
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error adding complaint '" + complaint.getWorkOrderId() + "': " + e.getMessage());
            return false;
        }
    }

    public Complaint getComplaint(String workOrderId) {
        String sql = "SELECT * FROM " + DatabaseContract.ComplaintEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.ComplaintEntry.COLUMN_WORK_ORDER_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, workOrderId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return resultSetToComplaint(rs);
                }
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting complaint '" + workOrderId + "': " + e.getMessage());
        }
        return null;
    }

    public List<Complaint> getAllComplaints() {
        List<Complaint> complaints = new ArrayList<>();
        String sql = "SELECT * FROM " + DatabaseContract.ComplaintEntry.TABLE_NAME;
        try (Connection conn = db.connect();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                complaints.add(resultSetToComplaint(rs));
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting all complaints: " + e.getMessage());
        }
        return complaints;
    }

    public List<Complaint> getComplaintsByOwner(String ownerPhone) {
        List<Complaint> complaints = new ArrayList<>();
        String sql = "SELECT * FROM " + DatabaseContract.ComplaintEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.ComplaintEntry.COLUMN_OWNER_PHONE_FK + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, ownerPhone);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    complaints.add(resultSetToComplaint(rs));
                }
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting complaints for owner '" + ownerPhone + "': " + e.getMessage());
        }
        return complaints;
    }

    public boolean updateComplaint(Complaint complaint) {
        // 重构: SQL语句增加 image_id 列
        String sql = "UPDATE " + DatabaseContract.ComplaintEntry.TABLE_NAME + " SET " +
                DatabaseContract.ComplaintEntry.COLUMN_MESSAGE + " = ?, " +
                DatabaseContract.ComplaintEntry.COLUMN_IMAGE_ID_FK + " = ?, " +
                DatabaseContract.ComplaintEntry.COLUMN_OWNER_PHONE_FK + " = ? WHERE " +
                DatabaseContract.ComplaintEntry.COLUMN_WORK_ORDER_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, complaint.getMessage());
            pstmt.setString(2, complaint.getImageId()); // 新增字段
            pstmt.setString(3, complaint.getOwnerPhone());
            pstmt.setString(4, complaint.getWorkOrderId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error updating complaint '" + complaint.getWorkOrderId() + "': " + e.getMessage());
            return false;
        }
    }

    public boolean deleteComplaint(String workOrderId) {
        String sql = "DELETE FROM " + DatabaseContract.ComplaintEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.ComplaintEntry.COLUMN_WORK_ORDER_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, workOrderId);
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error deleting complaint '" + workOrderId + "': " + e.getMessage());
            return false;
        }
    }
}